﻿using System.ComponentModel;
using System.Threading.Tasks;

namespace AMEE.MVVMBase
{
    /// <summary>
    /// Base class, providing Property Change events.<para>
    /// Derived classes can use the inherited OnPropertyChange method to fire the PropertyChanged event.</para>
    /// </summary>
    public class PropertyChangedBase : INotifyPropertyChanged
    {
        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected virtual void OnPropertyChanged(string propertyName) {
            Task.Run(() => {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                    handler(this, new PropertyChangedEventArgs(propertyName));
            });
        }
    }
}
